extends CharacterBody2D

const size = 2

var dir = Vector2(0, 0)
var speed = 0
var char = null
var type = 0

@onready var sounds = [[preload("res://1/sound/Happy Bleep-bloop0.wav"),preload("res://1/sound/Happy Bleep-bloop1.wav"),preload("res://1/sound/Happy Bleep-bloop2.wav")],[preload("res://1/sound/Angry Bleep-bloop0.wav"),preload("res://1/sound/Angry Bleep-bloop1.wav"),preload("res://1/sound/Angry Bleep-bloop2.wav")],[preload("res://1/sound/Tsundere Bleep-bloop0.wav"),preload("res://1/sound/Tsundere Bleep-bloop1.wav"),preload("res://1/sound/Tsundere Bleep-bloop2.wav")],[preload("res://1/sound/Happy Bleep-bloopBubbled.wav"), preload("res://1/sound/Angry Bleep-bloopBubbled.wav"), preload("res://1/sound/Tsundere Bleep-bloopBubbled.wav")]]

func _ready():
	char = get_node("/root/Main/Character")
	type = randi_range(1, 3)
	$AnimatedSprite2D.animation = str(type)
	$AnimatedSprite2D.play()
	dir = (char.global_position - global_position).normalized()
	dir = dir.rotated(randf_range(-PI/2, PI/2))
	speed = 25
	velocity = dir*speed

func collide(x):
	if "debris" in x.name or x.name == "Character" or "alien" in x.name:
		$AudioStreamPlayer2D.stream = sounds[type][randi_range(0, 2)]
		$AudioStreamPlayer2D.play()
		x.velocity = (x.global_position - global_position).normalized()*(6-size)*75
		
func _physics_process(delta: float) -> void:
	if move_and_slide() == true:
		var z = get_last_slide_collision().get_collider()
		z.collide(self)
		collide(z)
	if global_position.distance_to(char.global_position) > 10000:
		queue_free()
